package com.detect.server.handler;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.detect.common.context.BaseContext;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * 公共字段填充
 *
 * @author zhulj
 * @since 2024/1/12 14:43
 **/
@Slf4j
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {

    @Override
    public void insertFill(MetaObject metaObject) {
        this.strictInsertFill(metaObject, "isDeleted", Integer.class,0);
        this.strictInsertFill(metaObject, "createUserName", String.class, BaseContext.getCurrentBaseUser().getName());
        this.strictInsertFill(metaObject, "createUserId", Long.class, BaseContext.getCurrentBaseUser().getId());
        this.strictInsertFill(metaObject, "createTime", LocalDateTime.class, LocalDateTime.now());
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        this.strictInsertFill(metaObject, "updateUserId", Long.class, BaseContext.getCurrentBaseUser().getId());
        this.strictInsertFill(metaObject, "updateUserName", String.class, BaseContext.getCurrentBaseUser().getName());
        this.strictInsertFill(metaObject, "updateTime", LocalDateTime.class, LocalDateTime.now());
    }
}
